package com.qhl.dmscq;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.io.File;
import java.util.Objects;

public class MpGenerator {

    /**
     * <p>
     * MySQL 生成演示
     * </p>
     */
    public static void main(String[] args) {

        // 下面是可以更改的配置
        // 生成者
        String author = "hilinos";
        // 包名
        String pkg = "com.hilinos.base.user";
        // 生成文件存放地址
        String outDir = "/home/qhl/Desktop/code/";
        // 数据库配置
        String dbUrl = "jdbc:mysql://localhost:3306/hilinos?allowPublicKeyRetrieval=true";
        String username = "root";
        String password = "123456";

        AutoGenerator mpg = new AutoGenerator();
        // 选择 freemarker 引擎，默认 Veloctiy
        // mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setFileOverride(true);
        gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setAuthor(author);

        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        gc.setServiceName("%sService");
        gc.setControllerName("%sController");
        gc.setMapperName("%sMapper");
        gc.setServiceImplName("%sServiceImpl");
        gc.setOutputDir(outDir);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert() {
            // 自定义数据库表字段类型转换【可选】
            @Override
            public DbColumnType processTypeConvert(String fieldType) {
                System.out.println("转换类型：" + fieldType);
                return super.processTypeConvert(fieldType);
            }
        });
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(username);
        dsc.setPassword(password);
        dsc.setUrl(dbUrl);
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityColumnConstant(true);
        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(pkg);
        pc.setController("controller");
        pc.setEntity("domain.entity.pojo");
        pc.setMapper("domain.mapper");
        pc.setServiceImpl("domain.service.impl");
        pc.setService("domain.service");
        mpg.setPackageInfo(pc);

        TemplateConfig tc = new TemplateConfig();
        tc.setController("/templates/controller-mysql.java.vm");
        tc.setService("/templates/service-mysql-interface.java.vm");
        tc.setServiceImpl("/templates/service-mysql-impl.java.vm");
        tc.setEntity("/templates/entity-mysql.java.vm");
        tc.setMapper("/templates/mapper-mysql.java.vm");
        tc.setXml("");
        mpg.setTemplate(tc);

        mpg.execute();

        File entityDir = new File(gc.getOutputDir() + File.separator + pc.getParent().replace(".", File.separator) + File.separator
                + pc.getEntity().replace(".", File.separator));
        System.out.println(entityDir.getPath());
        for (File file : Objects.requireNonNull(entityDir.listFiles())) {
            if (file.getName().endsWith("Po.java")) {
                continue;
            }
            file.renameTo(new File(file.getPath().replace(".java", "PoJo.java")));
            System.out.println(file.getPath());
        }
    }

}